<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goods_m extends CI_Model {

	const TBL = 'goods';

	/**
     * 构造函数
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
       parent::__construct();
    }

    function get_good($good_id){
		$ret = array();

		$this->db->where('id',$good_id);
		$query = $this->db->get(self::TBL);
		return $query->row_array();
	}

    public function get_lev2_category_goods_count($city_id,$is_active=true){
    	$city_id = intval($city_id);
    	$ret = array();

    	$now = date("Y-m-d H:i:s",time());
    	$sql = "select cat_level2 as cat, count(*) as num from goods where city_id=$city_id and is_publish=1";
    	$sql .= $is_active ? (" and start_time<='$now' and close_time>='$now'") : " and close_time<'$now'";
    	$sql .= " group by cat_level2";

    	$query = $this->db->query($sql);
    	foreach ($query->result_array() as $row){
    		if(null !== $row['cat']){
				$ret[$row['cat']] = $row['num'];
    		}
		}

		return $ret;
    }

    public function goods_score_sort($a , $b){
    	$weight1 = $a['weight'];
    	$weight2 = $b['weight'];
    	
    	$discount1 = (isset($a['original_price']) && is_numeric($a['original_price']) && $a['original_price']>0) 
    					? ($a['sale_price']/$a['original_price']) : 1;
    	$discount2 = (isset($b['original_price']) && is_numeric($b['original_price']) && $b['original_price']>0) 
    					? ($b['sale_price']/$b['original_price']) : 1;
    	
    	$lefttime1 = (int)((strtotime($a['close_time'])-time())/3600);
    	$lefttime1 = ($lefttime1 < 0 || $lefttime1 > 24) ? 24 : $lefttime1;
    	$lefttime2 = (int)((strtotime($b['close_time'])-time())/3600);
    	$lefttime2 = ($lefttime2 < 0 || $lefttime2 > 24) ? 24 : $lefttime2;
    	
    	$score1 = SITE_INDEX * $weight1 / 10 + DISCOUNT_INDEX * (1 - $discount1) 
    			+ TIME_INDEX * ( 24 - $lefttime1) / 24;
    	$score2 = SITE_INDEX * $weight2 / 10 + DISCOUNT_INDEX * (1 - $discount2) 
    			+ TIME_INDEX * ( 24 - $lefttime2) / 24;
    	
    	$ret = $score2 - $score1;
    	return $ret == 0 ? 0 : ($ret > 0 ? 1 : -1);
    }
    
    public function get_goods($city_id, $cat_list = array(), $isvalid=true,
	    	$order = 'sale_price asc, sale_rate asc, create_time desc',
	    	$offset = 0, $limit = NUM_PER_PAGE,
	    	$site_list = 'all',
	    	$start_price = 0, $end_price = -1,
	    	$start_discount = 0, $end_discount = -1,
	    	$start_timeleft=0, $end_timeleft=-1
	    	){

    	if(!is_array($cat_list) || !$cat_list){
    		return array();
    	}
    	$city_id = intval($city_id);

    	$now = date("Y-m-d H:i:s",time());
    	$this->db->where('city_id', $city_id);
    	$this->db->where('is_publish', 1);
    	if($isvalid){
	    	$this->db->where('start_time <=', $now);
	    	if($end_timeleft > 0){
		    	$this->db->where('close_time >=', date("Y-m-d H:i:s",strtotime("+$start_timeleft day")));
		    	$this->db->where('close_time <=', date("Y-m-d H:i:s",strtotime("+$end_timeleft day")));
	    	}else{
	    		$this->db->where('close_time >=', $now);
	    	}
    	}else {
    		$this->db->where('close_time <', $now);
    	}
    	if($end_price > 0){
	    	$this->db->where('sale_price >=', $start_price);
	    	$this->db->where('sale_price <=', $end_price);
    	}
    	if($end_discount > 0){
	    	$this->db->where('sale_rate >=', $start_discount/10);
	    	$this->db->where('sale_rate <=', $end_discount/10);
    	}
		if($site_list!='all' && is_array($site_list)){
			$this->db->where_in('site_id',$site_list);
		}

    	$this->db->where_in('cat_level2',$cat_list);

    	if($limit > 0){
    		$this->db->limit($limit,$offset);
    	}

    	$this->db->select(self::TBL.'.*');
    	$this->db->from(self::TBL);
    	if($order){
    		$this->db->order_by(trim($order));
    	}else{
    		$this->db->select('site.weight');
			$this->db->join('site', (self::TBL).'.site_id = site.id');
    	}
    	
    	$query = $this->db->get();
    	if($query){
			$ret = $query->result_array();
			$query->free_result();
//			var_dump($ret);
			if(!$order){
				usort($ret, array($this, 'goods_score_sort'));
			}
//			var_dump($ret);
			return $ret;
		}

		return array();
    }

	public function is_city_with_goods($city_id){
		$now = date("Y-m-d H:i:s",time());
		$this->db->where('city_id', $city_id);
		$this->db->where('close_time <', $now);
		$this->db->from(self::TBL);
		return $this->db->count_all_results();
	}

    public function get_goods_count($city_id, $cat_list = array(),$isvalid=true,
    		$site_list = 'all',
    		$start_price = 0, $end_price = -1,
	    	$start_discount = 0, $end_discount = -1,
	    	$start_timeleft=0, $end_timeleft=-1
	    ){
    	if(!is_array($cat_list) || !$cat_list){
    		return 0;
    	}

    	$city_id = intval($city_id);
    	if($city_id <= 0){
    		return 0;
    	}

    	$now = date("Y-m-d H:i:s",time());
    	$this->db->where('city_id', $city_id);
    	$this->db->where('is_publish', 1);
    	if($isvalid){
	    	$this->db->where('start_time <=', $now);
	    	if($end_timeleft > 0){
		    	$this->db->where('close_time >=', date("Y-m-d H:i:s",strtotime("+$start_timeleft day")));
		    	$this->db->where('close_time <=', date("Y-m-d H:i:s",strtotime("+$end_timeleft day")));
	    	}else{
	    		$this->db->where('close_time >=', $now);
	    	}
    	}else {
    		$this->db->where('close_time <', $now);
    	}
    	if($end_price > 0){
	    	$this->db->where('sale_price >=', $start_price);
	    	$this->db->where('sale_price <=', $end_price);
    	}
    	if($end_discount > 0){
	    	$this->db->where('sale_rate >=', $start_discount/10);
	    	$this->db->where('sale_rate <=', $end_discount/10);
    	}
		if($site_list!='all' && is_array($site_list)){
			$this->db->where_in('site_id',$site_list);
		}

    	$this->db->where_in('cat_level2',$cat_list);
    	$this->db->from(self::TBL);

    	return $this->db->count_all_results();
    }

    public function get_city_goods_count($city_id,$isvalid=true){

    	$city_id = intval($city_id);
    	if($city_id <= 0){
    		return 0;
    	}

    	$now = date("Y-m-d H:i:s",time());
    	$this->db->where('city_id', $city_id);
    	$this->db->where('is_publish', 1);
    	if($isvalid){
	    	$this->db->where('start_time <=', $now);
	    	$this->db->where('close_time >=', $now);
    	}else{
    		$this->db->where('close_time <', $now);
    	}
    	$this->db->from(self::TBL);

    	return $this->db->count_all_results();
    }

	function get_site_total_goods_count($site_id){
		if ($site_id > 0){
			$now = date("Y-m-d H:i:s",time());
			$this->db->where('site_id', $site_id);
			$this->db->where('close_time >', $now);
			$this->db->from(self::TBL);
			return $this->db->count_all_results();
		} else {
			return -1;
		}
	}

    function get_site_goods_count($site_id = 0, $is_publish = false,$keyword=''){
    	$now = date("Y-m-d H:i:s",time());
		$this->db->where('close_time >', $now);
    	$this->db->where('is_publish', $is_publish?1:0);
    	if($site_id > 0){
    		$this->db->where('site_id', $site_id);
    	}
    	if(strlen($keyword) > 0){
    		$this->db->like('title', $keyword);
    	}
    	$this->db->from(self::TBL);

    	return $this->db->count_all_results();
    }

    function get_site_goods($site_id = 0, $is_publish = false,$keyword='',
    	$offset = 0, $limit = NUM_PER_PAGE, $order = 'id desc'){

    	$now = date("Y-m-d H:i:s",time());
		$this->db->where('close_time >', $now);
    	$this->db->where('is_publish', $is_publish?1:0);
    	if($site_id > 0){
    		$this->db->where('site_id', $site_id);
    	}
    	if(strlen($keyword) > 0){
    		$this->db->like('title', $keyword);
    	}
    	
    	if($order){
			$this->db->order_by(trim($order));
    	}
		$this->db->limit($limit,$offset);
		
    	$query = $this->db->get(self::TBL);
		return  $query ? $query->result_array() : array();
    }

    function delete_good($good_id){
    	$this->db->where('id', $good_id);
		return $this->db->delete(self::TBL);
    }

    function set_good($good_id, $data){
    	$this->db->where('id', $good_id);
		return $this->db->update(self::TBL,$data);
    }

    function set_goods_publish($site_id=0,$keyword='',$is_publish){
    	$where = array();
    	if($site_id > 0){
    		$where[] = " site_id=".intval($site_id);
    	}
    	if($keyword){
    		$where[] = " title like ".$this->db->escape('%'.$keyword.'%')."";
    	}

    	$where_str = '';
    	if(!empty($where)){
    		$where_str = implode(' and ', $where);
    		$where_str = ' where '.$where_str;
    	}

    	$sql = 'update '.self::TBL.' set is_publish='.($is_publish?1:0).$where_str;

    	return $this->db->query($sql);
    }
}

/* End of file users_mdl.php */
/* Location: ./application/models/users_mdl.php */
